/***************************************************************************
 创建者：       hualei
 开始时间：   2018.09.10
 copyright:(C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "../../programView/programview.h"
#include "modbusreadinstructionview.h"
#include "ui_modbusreadinstructionview.h"

ModbusReadInstructionView::ModbusReadInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusReadInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
}

void ModbusReadInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void ModbusReadInstructionView::getInput()
{
    noteStr = ui->note_lineEdit->text();
    regAddress = ui->RegAddress_Edit->text().toInt();
    regReadValue = ui->RegReadVelue_Edit->text().toInt();
    JMP_LB_Value = ui->JMP_LB_Edit->text();

    int internetRs485Index = ui->INTERNET_RS485_ComBox->currentIndex();
    int masterSlaveIndex = ui->MASTER_SLAVE_ComboBox->currentIndex();

    if((internetRs485Index == 0) && (masterSlaveIndex == 0))
    {
        modbusType = E_MODBUS_TYPE_INTERNET_MASTER;
    }
    else if((internetRs485Index == 0) && (masterSlaveIndex == 1))
    {
        modbusType = E_MODBUS_TYPE_INTERNET_SLAVE;
    }
    else if((internetRs485Index == 1) && (masterSlaveIndex == 0))
    {
        modbusType = E_MODBUS_TYPE_RS485_MASTER;
    }
    else if((internetRs485Index == 1) && (masterSlaveIndex == 1))
    {
        modbusType = E_MODBUS_TYPE_RS485_SLAVE;
    }
    else
    {
        modbusType = E_MODBUS_TYPE_ERROR;
    }

    modbusId = ui->modbusID_Edit->text();
}

void ModbusReadInstructionView::updateView()
{
    ui->note_lineEdit->setText(noteStr);
    ui->RegAddress_Edit->setText(QString::number(regAddress));
    ui->RegReadVelue_Edit->setText(QString::number(regReadValue));
    ui->modbusID_Edit->setText(modbusId);

    if(modbusType == E_MODBUS_TYPE_INTERNET_MASTER)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
    }
    else if(modbusType == E_MODBUS_TYPE_INTERNET_SLAVE)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
    }
    else if(modbusType == E_MODBUS_TYPE_RS485_MASTER)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
    }
    else if(modbusType == E_MODBUS_TYPE_RS485_SLAVE)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
    }

    ui->JMP_LB_Edit->setText(JMP_LB_Value);
}

void ModbusReadInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/modbusReadViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

}

void ModbusReadInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote();
}

void ModbusReadInstructionView::on_save_Btn_clicked()
{
    getInput();

    emit finishEditInstruction();
    emit saveInstruction(instruction);
}

void ModbusReadInstructionView::on_forward_Btn_clicked()
{
    this->close();

    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}
